package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.*;
import com.woniuxy.mapper.MenuMapper;
import com.woniuxy.mapper.TUserMenuMapper;
import com.woniuxy.mapper.TWorkMapper;
import com.woniuxy.mapper.TrMapper;
import com.woniuxy.service.PermissionControlService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionControlServiceImpl implements PermissionControlService, SaasService {
    @Autowired
    private TWorkMapper tWorkMapper;
    @Autowired
    private TUserMenuMapper tUserMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Override
    public ResponseData<?> queryTrAllByDepId(PageParam<TWork> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

        List<TWork> tWorkList=tWorkMapper.queryByHDepId(pageParam.getParam());
        PageInfo pageInfo =new PageInfo(tWorkList);

        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> updateManage(TWork tWork) {
        List<TRoleMenu> tRoleMenuList = tUserMenuMapper.queryByUserId(tWork.getId());

        return new ResponseData<>().ok(tRoleMenuList);
    }

    @Override
    public ResponseData<?> queryByUserIdAndMenuName() {
        return null;
    }

    @Override
    public ResponseData<?> queryAll() {
        List<Menu> menuList = menuMapper.queryAll();
        List<Menu> parentMenu = menuList.stream()
                .filter(e -> e.getParentId().equals(-1))
                .collect(Collectors.toList());

        for (Menu menu : parentMenu) {
            menu.setChildMenu(getChildMenus(menu.getId(), menuList));
        }
        for (Menu menu : parentMenu) {
            menu.getChildMenu().forEach(e -> {
                e.setChildMenu(getChildMenus(e.getId(), menuList));
            });
        }
        return new ResponseData<>().ok(parentMenu);
    }
    private List<Menu> getChildMenus(Integer parentId, List<Menu> allMenus) {
        List<Menu> childMenus = new ArrayList<>();
        for (Menu menu : allMenus) {
            if (menu.getParentId().equals(parentId)) {

                menu.setChildMenu(getChildMenus(menu.getId(), allMenus));
                childMenus.add(menu);
            }
        }
        return childMenus;
    }


    @Override
    public ResponseData<?> queryAllMenus(Integer userId) {
        List<Menu> menus = menuMapper.queryAllMenus(userId);

        List<Menu> parentMenu = menus.stream()
                .filter(e -> e.getParentId().equals(-1))
                .collect(Collectors.toList());

        for (Menu menu : parentMenu) {
            menu.setChildMenu(getChildMenus(menu.getId(), menus));
        }
        for (Menu menu : parentMenu) {
            menu.getChildMenu().forEach(e -> {
                e.setChildMenu(getChildMenus(e.getId(), menus));
            });
        }
        return new ResponseData<>().ok(parentMenu);
    }

    @Override
    public ResponseData<?> update(TRoleMenu tRoleMenu) {
        Integer id=tRoleMenu.getId();
        tUserMenuMapper.deleteById(id);
        List<Integer> checkedKeys = tRoleMenu.getCheckedKeys();
        ArrayList<TRoleMenu> tRoleMenus = new ArrayList<>();
        for (Integer menuId : checkedKeys){
            TRoleMenu tRoleMenuNew = new TRoleMenu();
            tRoleMenuNew.setUserId(id);
            tRoleMenuNew.setMenuId(menuId);
            tRoleMenus.add(tRoleMenuNew);
        }
        tUserMenuMapper.insert(tRoleMenus);




        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> balDel(Integer id) {

        tUserMenuMapper.deleteById(id);

        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryMenuList(Integer userId) {
        List<Menu> menus = menuMapper.queryAllMenus(userId);
        List<Menu> menuList= (List<Menu>) redisTemplate.opsForHash().get("menu","menuId");
        //找出所有的父菜单
        List<Menu> parentMenu = menus.stream()
                .filter(e -> e.getParentId().equals(-1))
                .collect(Collectors.toList());
        //找出所有的子菜单
        for (Menu menu : parentMenu) {
            menu.setChildMenu(getChildMenus(menu.getId(), menus));
        }
        for (Menu menu : parentMenu) {
            menu.getChildMenu().forEach(e -> {
                e.setChildMenu(getChildMenus(e.getId(), menus));
            });
        }
        return  new ResponseData<>().ok();


    }
}
